import request from '@/utils/request';
import { requestWrapper, randomDate, randomInt, randomPick, paginate } from '@/utils/mock';

// 获取操作日志列表
export function getOperationLogList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/log/operation/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      // 操作模块列表
      const modules = ['用户管理', '角色管理', '菜单管理', '部门管理', '岗位管理', '字典管理', '系统设置'];
      // 操作类型列表 (1=新增, 2=修改, 3=删除, 4=授权, 5=导出, 6=导入, 7=强退, 8=清空数据)
      const businessTypes = [1, 2, 3, 4, 5, 6, 7, 8];
      // 请求方式
      const requestMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
      // 操作人员类型 (0=后台用户,1=手机端用户)
      const operatorTypes = ['0', '1'];
      // 操作状态 (0=异常,1=正常)
      const statusOptions = ['0', '1'];

      const operLogList = Array.from({ length: 30 }, (_, index) => {
        const title = randomPick(modules);
        const businessType = randomPick(businessTypes);
        const status = randomPick(statusOptions);
        
        return {
          id: index + 1,
          title,
          businessType,
          method: businessType === 1 ? 'POST' : (businessType === 2 ? 'PUT' : (businessType === 3 ? 'DELETE' : randomPick(requestMethods))),
          requestMethod: randomPick(requestMethods),
          operatorType: randomPick(operatorTypes),
          operName: randomPick(['admin', 'user1', 'user2', 'manager']),
          deptName: randomPick(['总公司', '研发部门', '市场部门', '财务部门']),
          operUrl: `/system/${title.split('管理')[0].toLowerCase()}${businessType === 1 ? '' : '/1'}`,
          operIp: `192.168.1.${randomInt(1, 255)}`,
          operLocation: '内网IP',
          operParam: businessType === 1 ? '{"name":"test","remark":"测试数据"}' : '{"id":1,"name":"test","remark":"测试数据"}',
          jsonResult: status === '1' ? '{"code":200,"msg":"操作成功"}' : '{"code":500,"msg":"操作失败"}',
          status,
          errorMsg: status === '0' ? '操作异常：空指针异常' : '',
          operTime: randomDate(-30)
        };
      });

      // 处理搜索条件
      let filteredLogs = [...operLogList];

      if (params) {
        if (params.title) {
          filteredLogs = filteredLogs.filter(item => 
            item.title.includes(params.title)
          );
        }
        
        if (params.operName) {
          filteredLogs = filteredLogs.filter(item => 
            item.operName.includes(params.operName)
          );
        }
        
        if (params.status !== undefined && params.status !== '') {
          filteredLogs = filteredLogs.filter(item => 
            item.status === params.status
          );
        }
        
        if (params.beginTime && params.endTime) {
          filteredLogs = filteredLogs.filter(item => {
            const operTime = new Date(item.operTime).getTime();
            const beginTime = new Date(params.beginTime).getTime();
            const endTime = new Date(params.endTime).getTime();
            return operTime >= beginTime && operTime <= endTime;
          });
        }
      }

      // 处理分页
      const pageNum = params?.pageNum ? parseInt(params.pageNum) : 1;
      const pageSize = params?.pageSize ? parseInt(params.pageSize) : 10;
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = pageNum * pageSize;
      const pageData = filteredLogs.slice(startIndex, endIndex);

      return paginate(pageData, pageNum, pageSize, filteredLogs.length);
    }
  );
}

// 删除操作日志
export function deleteOperationLog(ids) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/log/operation/${ids}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 清空操作日志
export function cleanOperationLog() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/log/operation/clean',
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 导出操作日志
export function exportOperationLog(params) {
  return request({
    url: '/system/log/operation/export',
    method: 'get',
    params,
    responseType: 'blob'
  });
}

// 获取登录日志列表
export function getLoginLogList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/log/login/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      // 浏览器
      const browsers = ['Chrome', 'Firefox', 'Safari', 'Edge', 'IE'];
      // 操作系统
      const osList = ['Windows 10', 'Windows 7', 'MacOS', 'Linux', 'Android', 'iOS'];
      // 登录状态 (0=登录失败,1=登录成功)
      const statusOptions = ['0', '1'];

      const loginLogList = Array.from({ length: 30 }, (_, index) => {
        const status = randomPick(statusOptions);
        
        return {
          id: index + 1,
          userName: randomPick(['admin', 'user1', 'user2', 'manager']),
          ipaddr: `192.168.1.${randomInt(1, 255)}`,
          loginLocation: '内网IP',
          browser: randomPick(browsers),
          os: randomPick(osList),
          status,
          msg: status === '1' ? '登录成功' : randomPick(['密码错误', '验证码错误', '账户已锁定']),
          loginTime: randomDate(-30)
        };
      });

      // 处理搜索条件
      let filteredLogs = [...loginLogList];

      if (params) {
        if (params.userName) {
          filteredLogs = filteredLogs.filter(item => 
            item.userName.includes(params.userName)
          );
        }
        
        if (params.ipaddr) {
          filteredLogs = filteredLogs.filter(item => 
            item.ipaddr.includes(params.ipaddr)
          );
        }
        
        if (params.status !== undefined && params.status !== '') {
          filteredLogs = filteredLogs.filter(item => 
            item.status === params.status
          );
        }
        
        if (params.beginTime && params.endTime) {
          filteredLogs = filteredLogs.filter(item => {
            const loginTime = new Date(item.loginTime).getTime();
            const beginTime = new Date(params.beginTime).getTime();
            const endTime = new Date(params.endTime).getTime();
            return loginTime >= beginTime && loginTime <= endTime;
          });
        }
      }

      // 处理分页
      const pageNum = params?.pageNum ? parseInt(params.pageNum) : 1;
      const pageSize = params?.pageSize ? parseInt(params.pageSize) : 10;
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = pageNum * pageSize;
      const pageData = filteredLogs.slice(startIndex, endIndex);

      return paginate(pageData, pageNum, pageSize, filteredLogs.length);
    }
  );
}

// 删除登录日志
export function deleteLoginLog(ids) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/log/login/${ids}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 清空登录日志
export function cleanLoginLog() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/log/login/clean',
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 导出登录日志
export function exportLoginLog(params) {
  return request({
    url: '/system/log/login/export',
    method: 'get',
    params,
    responseType: 'blob'
  });
} 